package com.un.ebs.inventory.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.inventory.domain.EntRelationExt;
import com.un.ebs.inventory.dto.EntRelationApiHandlerDto;
import com.un.ebs.inventory.dto.EntRelationDto;
import com.un.ebs.inventory.dto.EntRelationUpdateDto;
import com.un.ebs.inventory.enums.BusinessRelationTypeEnum;
import com.un.ebs.inventory.mapper.EntRelationMapper;
import com.un.ebs.inventory.service.BusinessRelationService;
import com.un.ebs.purchase.service.PurchaseOrderService;
import com.un.ebs.sys.domain.Ent;
import com.un.ebs.sys.dto.EntAddDto;
import com.un.ebs.sys.dto.EntUpdateDto;
import com.un.ebs.sys.service.EntService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liubo
 * @email liubocs@163.com
 * @date 2020/5/28 17:46
 */
@RestController
@RequestMapping("/entRelation")
@Api(value = "entRelation")
public class EntRelationController {

    @Resource
    private BusinessRelationService businessRelationService;

    @Resource
    private EntService entService;

    @PostMapping("addCustomer")
    @ApiOperation("添加，客户")
    public WebResult addCustomer(@RequestBody @Validated EntAddDto req) {
        businessRelationService.addCustomer(req);
        return WebUtils.success();
    }

    @PostMapping("addSupplier")
    @ApiOperation("添加，供应商")
    public WebResult addSupplier(@RequestBody @Validated EntAddDto req) {
        businessRelationService.addSupplier(req);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation("修改")
    public WebResult update(@RequestBody @Validated EntRelationUpdateDto req) {
        businessRelationService.update(req);
        return WebUtils.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    public WebResult delete(@RequestParam String id) {
        businessRelationService.delete(id);
        return WebUtils.success();
    }

    @PostMapping("setTempContractSend")
    @ApiOperation("临时合同发货设置")
    public WebResult setTempContractSend(@RequestParam String id) {
        businessRelationService.setTempContractSend(id);
        return WebUtils.success();
    }

    @PostMapping("setOpenApiUser")
    @ApiOperation("设置往来单位的 OpenApi Token")
    public WebResult setOpenApiUser(@RequestParam String id,@RequestParam String userId) {
        businessRelationService.setOpenApiUser(id,userId);
        return WebUtils.success();
    }

    @PostMapping("setApiHandler")
    @ApiOperation("设置往来单位的接口处理器")
    public WebResult setApiHandler(@RequestParam String entRelationId, @RequestParam String apiId, @RequestParam String handlerCode) {
        businessRelationService.setApiHandler(entRelationId, apiId, handlerCode);
        return WebUtils.success();
    }

    @PostMapping("deleteApiHandlerById")
    @ApiOperation("删除往来单位的接口处理器")
    public WebResult deleteApiHandlerById(@RequestParam String entRelationId) {
        businessRelationService.deleteApiHandlerById(entRelationId);
        return WebUtils.success();
    }

    @GetMapping("listEntRelationApiHandler")
    @ApiOperation("查询，往来单位的接口处理器")
    public WebResult<List<EntRelationApiHandlerDto>> queryApiHandler(@RequestParam String entRelationId) {
        return WebUtils.success(businessRelationService.queryApiHandler(entRelationId));
    }

    @GetMapping("entRelationApiHandler")
    @ApiOperation("查询一个往来单位的接口处理器")
    public WebResult<EntRelationApiHandlerDto> queryApiHandlerById(@RequestParam String id) {
        return WebUtils.success(businessRelationService.queryApiHandlerById(id));
    }

    @GetMapping("listCustomer")
    @ApiOperation("查询，客户")
    public WebResult<IPage<EntRelationExt>> listCustomer(@RequestParam(required = false) String searchText, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(businessRelationService.query(searchText, BusinessRelationTypeEnum.CUSTOMER, pageSize, pageIndex));
    }


    @GetMapping("listSupplier")
    @ApiOperation("查询，供应商")
    public WebResult<IPage<EntRelationExt>> listSupplier(@RequestParam(required = false) String searchText, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(businessRelationService.query(searchText, BusinessRelationTypeEnum.SUPPLIER, pageSize, pageIndex));
    }

    @GetMapping("/{id}")
    @ApiOperation("详情")
    public WebResult<EntRelationExt> queryById(@PathVariable String id) {
        return WebUtils.success(businessRelationService.queryById(id));
    }

    @GetMapping("list")
    @ApiOperation("查询")
    public WebResult<IPage<EntRelationDto>> list(@RequestParam(required = false) String searchText, @RequestParam(required = false) BusinessRelationTypeEnum type, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(businessRelationService.query(searchText, type, pageSize, pageIndex));
    }

}
